﻿
$(function () {

    $('#hname').attr("disabled", "disabled");
    $("#sitename").click(function () {//设置用户名
        var name = $('#name').val();
        if (name == "") {
            alert("不能为空！");
        }
        $("#Placeholder").value = name;
        $("#MyClientName").text(name);
        $('#name').attr("disabled", "disabled");
    });
    //var myClientName = $('#Placeholder').val();
    var myClientName = $("#MyClientName").text();
    //alert(myClientName);
    // Proxy created on the fly
    var chat = $.connection.chat;
    //点击用户对其说话
    $("#clientname").live("click", function () {
        var cname = $("#clientname").text();
        //alert(cname);
        $("#hname").attr("value", cname);
        $('#hname').attr("disabled", "disabled");
        //alert($("#hname").val());
    });
    var hname = $("#hname").val();
    $("#noname").click(function () {
        //alert(hname);
        $("#hname").attr("value", "");
        //$("#noname").attr("value", "");
    });
    // Declare a function on the chat hub so the server can invoke it
    chat.addSomeMessage = function (clientName, message) {

        hname = $("#hname").val();
        if (hname == "") {
            var mge = "对大家说：";
        } else {
            mge = "对：" + hname + "说：";
            //alert(mge);
            $("#noname").attr("value", "取消对其");
        }
        writeEvent('<b id="clientname">' + clientName + '</b> ' + mge + ' ' + message, 'event-message');
    };

    //send方法调用
    $("#broadcast").click(function () {
        var hcontent = editor.html();
        //alert(hcontent);
        myClientName = $("#MyClientName").text();
        // Call the chat method on the server
        chat.send(myClientName, hcontent)
                            .done(function () {
                                console.log('Sent message success!');
                            })
                            .fail(function (e) {
                                console.warn(e);
                            });
        
        editor.html('');
    });
    // Start the connection
    $.connection.hub.start();

    //A function to write events to the page
    function writeEvent(eventLog, logClass) {
        var now = new Date();
        var nowStr = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
        $('#messages').prepend('<li class=" + logClass + "><b>' + nowStr + '</b> ' + eventLog + '.</li>');
    }
});